// 输入一棵二叉树，判断该二叉树是否是平衡二叉树。
// 平衡二叉树：每个节点的左右子树的深度相差不能超过1
function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
}
var a = new TreeNode(1);
var b = new TreeNode(2);
var c = new TreeNode(3);
var d = new TreeNode(4);

a.left = b;
a.right = c;
c.left = d;
function TreeDepth(pRoot)
{
    // write code here
    if(pRoot == null){
        return 0;
    }
    let leftDep = TreeDepth(pRoot.left);
    let rightDep = TreeDepth(pRoot.right);
    return leftDep > rightDep ? (leftDep + 1 ):(rightDep + 1);
}
function IsBalanced_Solution(pRoot)
{
    // write code here
    if(pRoot == null)
        return true;
    let leftDep = TreeDepth(pRoot.left);
    let rightDep = TreeDepth(pRoot.right);
    let dis = leftDep - rightDep;
    if(dis > 1 || dis < -1){
        return false;
    }
    return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
}

console.log(IsBalanced_Solution(a));